Lab Assignment 6
Due date : September 17, 12 PM EST
Total Marks : 30
Today’s Exercise
Topics |
Lab Exercises |
Points |
Drawing a Face |
|
|
Important Note:
· Work with your partner collaboratively.
· Work on one computer only.
· Only one partner should upload all assignment files. The other partner will upload only the declaration form.
· Make sure that these two programs compile and run without any error. A program with compiler or runtime error will receive no credit.
The Java Coordinate System
The Java coordinate system is discussed in Section 2.7 & 2.9 of the text. Under this system, the upper left-hand corner of the window is the point (0,0). The X axis goes across the window, and the Y axis goes down the window. So the bigger the X value, the farther a point is to the right. The bigger the Y value, the farther it is down. There are no negative X or Y values in the Java coordinate system. Actually, you can use negative values, but since they're off the screen they won't show up!
1. Save files Coords.java and Coords.html to your directory. File Coords.java contains an applet that draws a rectangle whose upper lefthand corner is at 0,0. Use the applet viewer to run this applet. Remember that you have to do it through the html file: appletviewer Coords.html.
2. Modify the applet so that instead of 0,0 for the upper lefthand corner, you use the coordinates of the middle of the applet window. This applet is set up to be 600 pixels wide and 400 pixels high, so you can figure out where the middle is. Save, compile, and view your applet. Does the rectangle appear to be in the middle of the screen? Modify the coordinates so that it does appear to be in the middle.
3. Now add four more rectangles to the applet, one in each corner. Each rectangle should just touch one corner of the center rectangle and should go exactly to the edges of the window.
4. Make each rectangle be a different color. To do this, use the setColor method of the Graphics class to change the color (this is already done once). Do not change the background color once it has been set! Doing so causes the screen to flicker between colors.
// ****************************************************************
// Coords.java
//
// Draw rectangles to illustrate the Java coordinate system
//
// ****************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Coords extends JApplet
{
public void paint (Graphics page)
{
// Declare size constants
final int MAX_SIZE = 300;
final int PAGE_WIDTH = 600;
final int PAGE_HEIGHT = 400;
// Declare variables
int x, y; // x and y coordinates of upper left-corner of each shape
int width, height; // width and height of each shape
// Set the background color
setBackground (Color.yellow);
// Set the color for the next shape to be drawn
page.setColor (Color.blue);
// Assign the corner point and width and height
x = 0;
y = 0;
width = 150;
height = 100;
page.fillRect(x, y, width, height);
}
}
Coords.html
<html>
<applet code="Coords.class" width=600 height=400>
</applet>
</html>
Drawing a Face
Write an applet that draws a smiling face. Give the face eyes with pupils, ears, a nose, and a mouth. Use at least three different colors, and fill in some of the features. Name this file Face.java, and create a corresponding .html file. View your applet using the applet viewer.
Challenge Question for Bonus Point: 5 points extra
Now add your face to a web page you created (you may have created one in an earlier lab exercise). You will do this using the <APPLET> tag that is in the .html file you are using with the applet viewer—you can just copy it out of that file and paste it into your other file. The applet will appear wherever you insert the applet tag.
Submit the following files:
· Coords.java (the final version with all changes)
· Coords.html
· Face.java
· Face.html
· Your webpage html file if you do the challenge part
· Peer evaluation form